package com.guoxin.designpattern.chapter4;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guoxin
 * @date 2019/9/3
 */
public class Subject {
    private List<Observer> observerList = new ArrayList<>();
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        if (this.state == state) {
            return;
        }
        this.state = state;
        notifyAllObserver();
    }

    private void notifyAllObserver() {
        observerList.forEach(Observer::update);
    }

    public void addObserver(Observer observer) {
        observerList.add(observer);
    }

}
